package main

import (
	"fmt"
	"net"
)

func main5() {
	destIP := net.ParseIP("127.0.0.1")
	localAddress := &net.UDPAddr{IP: net.IPv4zero, Port: 2000}
	destAddress := &net.UDPAddr{IP: destIP, Port: 3000}

	// 拨号模式下，利用本地地址和远程地址，创建DUP conn对象
	conn, err := net.DialUDP("udp", localAddress, destAddress)

	if err != nil {
		fmt.Printf("拨号连接错误: %s", err)
		return
	}

	defer conn.Close()

	data := make([]byte, 1024)
	for {
		// 接收远端消息
		n, err := conn.Read(data)
		if err != nil {
			fmt.Printf("接收消息错误：%v\n", err)
			continue
		}
		fmt.Printf("收到消息：%s\n", data[:n])

		// 向远端发送消息
		n, err = conn.Write([]byte("ok"))
		if err != nil {
			fmt.Printf("接收消息错误：%v", err)
		}
	}
}
